define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ims/vlan/index',
                    add_url: 'ims/vlan/add',
                    edit_url: 'ims/vlan/edit',
                    del_url: 'ims/vlan/del',
                    multi_url: 'ims/vlan/multi',
                    table: 'ims_vlan',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'admin_id', title: __('Admin_id')},
                        {field: 'vlanid', title: __('Vlanid')},
                        {field: 'mac', title: __('Mac')},
                        {field: 'ip', title: __('Ip')},
                        {field: 'mask', title: __('Mask')},
                        {field: 'gateway', title: __('Gateway')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.btn-restart').click(function() {
                    var element = this;
                    Layer.confirm(
                        __('重启FreeSwitch将中断所有通话，<br/>请谨慎操作！'),
                        {icon: 2, title: __('Warning'), offset: 0, shadeClose: true},
                        function (index) {
                            var options = table.bootstrapTable('getOptions');
                            //var data = element ? $(element).data() : {};
                            var url = 'ims/vlan/restartfs';
                            url = Table.api.replaceurl(url, {ids: null}, table);
                            //var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
                            var action = null;
                            var params = null;
                            var options = {url: url, data: {action: action, params: params}};
                            Fast.api.ajax(options, function (data, ret) {
                                var success = $(element).data("success") || $.noop;
                                if (typeof success === 'function') {
                                    if (false === success.call(element, data, ret)) {
                                        return false;
                                    }
                                }
                                table.bootstrapTable('refresh');
                            }, function (data, ret) {
                                //var error = $(element).data("error") || $.noop;
                                var error = $.noop;
                                if (typeof error === 'function') {
                                    if (false === error.call(element, data, ret)) {
                                        return false;
                                    }
                                }
                            });
                            Layer.close(index);
                        }
                    );
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
